package yxy.game.pm2.bean;

import org.redisson.api.RBucket;
import yxy.apple.util.redisson.RedisConnection;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.game.base.redis.RedisManager;
import yxy.cherry.game.base.redis.RedisType;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;

import java.util.ArrayList;

public class Format {
    static public enum FormatType {
        普通(1),
        防守(2),
        中原擂台(3),
        魏国塔(4),
        蜀国塔(5),
        吴国塔(6),
        群国塔(7),
        斩关夺隘(8)
        //翻牌联盟战要5个布阵,先占个位置9-13
        ;

        private int number;

        private FormatType(int number) {
            this.number = number;
        }

        public int getNumber() {
            return number;
        }

        static public FormatType forNumber(int number) {
            switch (number) {
//			case 2:
//				return 防守;
                default:
                    return 普通;
            }
        }

        static public FormatType CampTowerForNumber(int number) {
                if(number == 4){
                    return 魏国塔;
                }else if(number == 5){
                    return 蜀国塔;
                }else if(number == 6){
                    return 吴国塔;
                }else if(number == 7){
                    return 群国塔;
                }else{
                    return null;
                }
        }



        public static ArrayList<FormatType> getFormatTypes() {
            ArrayList<FormatType> formatTypes = new ArrayList<>();
            formatTypes.add(FormatType.普通);
            formatTypes.add(FormatType.防守);
            formatTypes.add(FormatType.魏国塔);
            formatTypes.add(FormatType.蜀国塔);
            formatTypes.add(FormatType.吴国塔);
            formatTypes.add(FormatType.群国塔);
            formatTypes.add(FormatType.中原擂台);
            formatTypes.add(FormatType.斩关夺隘);
            return formatTypes;
        }
    }

    // bean===========================================================================
    private String fu;
    private String fm;
    private String fd;
    private String bu;
    private String bm;
    private String bd;
    private int nb; //神器

    private int isAfreshPower; //判断是否需要重新计算战力

    public int getIsAfreshPower() {
        return isAfreshPower;
    }

    public void setIsAfreshPower(int isAfreshPower) {
        this.isAfreshPower = isAfreshPower;
    }

    public String getFu() {
        return fu;
    }

    public void setFu(String fu) {
        this.fu = fu;
    }

    public String getFm() {
        return fm;
    }

    public void setFm(String fm) {
        this.fm = fm;
    }

    public String getFd() {
        return fd;
    }

    public void setFd(String fd) {
        this.fd = fd;
    }

    public String getBu() {
        return bu;
    }

    public void setBu(String bu) {
        this.bu = bu;
    }

    public String getBm() {
        return bm;
    }

    public void setBm(String bm) {
        this.bm = bm;
    }

    public String getBd() {
        return bd;
    }

    public void setBd(String bd) {
        this.bd = bd;
    }

    public int getNb() {
        return nb;
    }

    public void setNb(int nb) {
        this.nb = nb;
    }

    // func==========================================================================
    public boolean ismember(String heroCode) {
        if (heroCode == null) {
            return false;
        }
        if (getFu() != null && getFu().equals(heroCode)) {
            return true;
        }
        if (getFm() != null && getFm().equals(heroCode)) {
            return true;
        }
        if (getFd() != null && getFd().equals(heroCode)) {
            return true;
        }
        if (getBu() != null && getBu().equals(heroCode)) {
            return true;
        }
        if (getBm() != null && getBm().equals(heroCode)) {
            return true;
        }
        if (getBd() != null && getBd().equals(heroCode)) {
            return true;
        }
        return false;
    }

    public String toString() {
        return "fu: " + fu + " fm: " + fm + " fd: " + fd + " bu: " + bu + " bm: " + bm + " bd: " + bd;
    }

    // redis==========================================================================

    static public String key(String playerCode, FormatType type) {
        return String.format("PLAYER:%s:FORMAT:%s", playerCode, String.valueOf(type.getNumber()));
    }

    static private RedisConnection redis() {
        return RedisManager.getInstance().getConnection(RedisType.REDIS_GAME);
    }

    static private <V> RBucket<V> touch(String key) throws RedisException {
        return RedisManager.client(redis()).getBucket(key);
    }

    static public Format get(String playerCode, FormatType type) {
        try {
            RBucket<Format> bucket = touch(key(playerCode, type));
            Format module = bucket.get();
            if (module == null) {
                module = MySQLCacheDAO.getV(key(playerCode, type), Format.class);
                if (module != null) {
                    module.set(playerCode, type, module);
                }//同步到redis
            }
            return module;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //存储到数据库的方法
    public static void saveToDB(String playerCode) {
        try {
            ArrayList<FormatType> formatTypes = FormatType.getFormatTypes();

            for (FormatType formatType : formatTypes) {
                String key = key(playerCode, formatType);
                RBucket<Format> bucket = touch(key);
                if (bucket.isExists()) {
                    Format module = bucket.get();
                    MySQLCacheDAO.setV(key, module);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //从redis移走数据的操作
    public static void removeAllFromRedis(String playerCode) {
        try {
            ArrayList<FormatType> formatTypes = FormatType.getFormatTypes();

            for (FormatType formatType : formatTypes) {
                touch(key(playerCode, formatType)).delete();
            }
        } catch (RedisException e) {
            e.printStackTrace();
        }
    }

    static public void set(String playerCode, FormatType type, Format format) {
        try {
            RBucket<Format> bucket = touch(key(playerCode, type));
            bucket.set(format);
        } catch (RedisException e) {
            e.printStackTrace();
        }
    }

}
